
package com.jf.cloud.multishop.controller.admin;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.TemplateType;
import com.jf.cloud.multishop.dto.ShopTemplateDTO;
import com.jf.cloud.multishop.model.ShopTemplate;
import com.jf.cloud.multishop.service.ShopTemplateService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;

/**
 * 商品装修模板表
 * @author zz
 */
@RestController("multishopShopTemplateController")
@RequestMapping("/mp/shop_template")
@Tag(name = "店铺装修模板")
public class ShopTemplateController {

    @Autowired
    private ShopTemplateService shopTemplateService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/page")
    @Operation(summary = "根据类型获取商品装修模板表列表" , description = "根据类型分页获取商品装修模板表列表")
    public ServerResponseEntity<PageVO<ShopTemplate>> page(@Valid PageDTO pageDTO, @ParameterObject ShopTemplateDTO shopTemplateDTO) {
        shopTemplateDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<ShopTemplate> shopTemplatePage = shopTemplateService.page(pageDTO, mapperFacade.map(shopTemplateDTO, ShopTemplate.class));
        return ServerResponseEntity.success(shopTemplatePage);
    }

    @GetMapping
    @Operation(summary = "获取商品装修模板表" , description = "根据templateId获取商品装修模板表")
    public ServerResponseEntity<ShopTemplate> getByTemplateId(@ParameterObject @RequestParam Long templateId) {
        return ServerResponseEntity.success(shopTemplateService.getByTemplateId(templateId));
    }

    @PostMapping("/save_pc")
    @Operation(summary = "保存商品pc装修模板表" , description = "保存商品pc装修模板表")
    public ServerResponseEntity<Long> save(@Valid @RequestBody ShopTemplateDTO shopTemplateDTO) {
        ShopTemplate shopTemplate = mapperFacade.map(shopTemplateDTO, ShopTemplate.class);
        shopTemplate.setShopId(AuthUserContext.get().getTenantId());
        shopTemplate.setType(TemplateType.PC.value());
        shopTemplate.setTemplateId(null);
        shopTemplateService.save(shopTemplate);
        return ServerResponseEntity.success(shopTemplate.getTemplateId());
    }

    @PostMapping("/save_h5")
    @Operation(summary = "保存商品h5装修模板表" , description = "保存商品h5装修模板表")
    public ServerResponseEntity<Long> saveH5(@Valid @RequestBody ShopTemplateDTO shopTemplateDTO) {
        ShopTemplate shopTemplate = mapperFacade.map(shopTemplateDTO, ShopTemplate.class);
        shopTemplate.setShopId(AuthUserContext.get().getTenantId());
        shopTemplate.setType(TemplateType.H5.value());
        shopTemplate.setTemplateId(null);
        shopTemplateService.save(shopTemplate);
        return ServerResponseEntity.success(shopTemplate.getTemplateId());
    }

    @PostMapping("/copy_pc")
    @Operation(summary = "PC端复制模板信息" , description = "PC端复制模板信息")
    public ServerResponseEntity<Long> copy(@RequestParam Long templateId) {
        ShopTemplate shopTemplate = copyTemplate(templateId);
        return ServerResponseEntity.success(shopTemplate.getTemplateId());
    }

    @PostMapping("/copy_h5")
    @Operation(summary = "移动端复制模板信息" , description = "移动端复制模板信息")
    public ServerResponseEntity<Long> copyH5(@RequestParam Long templateId) {
        ShopTemplate shopTemplate = copyTemplate(templateId);
        return ServerResponseEntity.success(shopTemplate.getTemplateId());
    }

    private ShopTemplate copyTemplate(Long templateId) {
        ShopTemplate shopTemplate = shopTemplateService.getByTemplateId(templateId);
        if (Objects.isNull(shopTemplate)) {
            throw new LuckException("模板不存在，请刷新页面重试");
        }
        if (!Objects.equals(shopTemplate.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopTemplate.setTemplateId(null);
        shopTemplate.setName(shopTemplate.getName() + "副本");
        shopTemplate.setCreateTime(new Date());
        shopTemplate.setUpdateTime(new Date());
        shopTemplateService.save(shopTemplate);
        return shopTemplate;
    }
}
